package com.yenlien.traceyun.milk.controller;

import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.business.utils.EasyPoiUtils;
import com.yenlien.traceyun.milk.entity.Farmers;
import com.yenlien.traceyun.milk.service.FarmersService;
import com.yenlien.traceyun.webase.controller.BaseController;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Date 2022/7/11
 * @Author jiaLong
 * @Descript 农户管理
 */
@Api(tags = "农户管理")
@RestController
@RequestMapping("/farmers")
public class FarmersController extends BaseController {

    @Autowired
    private FarmersService farmersService;

    @PostMapping(value = "/save")
    public Object save(@RequestBody Farmers farmers) {
        return success(farmersService.save(farmers));
    }

    @PostMapping(value = "/findByList")
    public Object findByList(@RequestBody Farmers farmers) {
        if(StringUtils.isBlank(farmers.getCompanyId())){
            farmers.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        }
        return success(farmersService.findByList(farmers));
    }


    @PostMapping(value = "/export")
    public void exportTemplate(HttpServletResponse response) {
        try {
            EasyPoiUtils.exportExcel(farmersService.findAll(), "农户数据", "农户数据", Farmers.class, "农户数据", response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }





}
